package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.enums.PaymentWay;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 李旭
 * @Time: 15:06
 * @Description:
 *
 *   支付宝相关 服务
 */
@Service
public class AliPayServiceImpl implements AlipayService {


    @Autowired
    private PaymentInfoService paymentInfoService;
    @Autowired
    private AlipayClient alipayClient;

    //获取支付宝的支付页面 字符串
    @Override
    public String submit(Long orderId) {

        //支付微服务
        //1、创建支付信息表
        PaymentInfo paymentInfo = paymentInfoService.insert(orderId, PaymentType.ALIPAY);
        //2、连接支付宝
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        Map map = new HashMap();
        //业务参数
        map.put("out_trade_no",paymentInfo.getOutTradeNo());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount",paymentInfo.getTotalAmount());
        map.put("subject",paymentInfo.getSubject());
        request.setBizContent(JSONObject.toJSONString(map));

        //设置同步地址
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //设置异步地址
        request.setNotifyUrl(AlipayConfig.notify_payment_url);

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println(response.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        //返回值  就是支付页面
        return response.getBody();
    }

    //退款
    @Override
    public void refund(String outTradeNo) {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        Map map = new HashMap();
        map.put("refund_amount","4999");
        map.put("out_trade_no",outTradeNo);
        request.setBizContent(JSONObject.toJSONString(map));
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("退款成功！调用成功");
        } else {
            System.out.println("调用失败");
        }
    }


}
